package com.zw.se2.general;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

public class CleanerScheduler {

    public void main_quartz() {
        Log logger = LogFactory.getLog(CleanerScheduler.class);

        try {
            // Create a Scheduler and schedule the Job
            Scheduler scheduler = createScheduler();
            // Jobs can be scheduled after Scheduler is running
            scheduler.start();
            logger.info("定时获取信息 started at " + new Date());
            try {
                //启动QUARTZ任务，每1分钟启动一次清理topic进程
                scheduleJob(scheduler, "TopicCleanGetJob", TopicCleanService.class, 60);
            } catch (Exception e) {
                System.out.println(e);
            }

        } catch (SchedulerException ex) {
            logger.error(ex);
        }

    }

    public Scheduler createScheduler() throws SchedulerException {//创建调度器

        return StdSchedulerFactory.getDefaultScheduler();

    }


    private void scheduleJob(Scheduler scheduler, String jobName, Class jobClass, int scanInterval) throws SchedulerException {

        // Create a JobDetail for the Job
        JobDetail jobDetail = new JobDetail(jobName, Scheduler.DEFAULT_GROUP, jobClass);

        // Configure the directory to scan

        // jobDetail.getJobDataMap().put("devicesStr", devicesStr);

        Trigger trigger = TriggerUtils.makeSecondlyTrigger(scanInterval);
        trigger.setName(jobName + "-Trigger");

        // Start the trigger firing from now

        trigger.setStartTime(new Date());

        // Associate the trigger with the job in the scheduler

        scheduler.scheduleJob(jobDetail, trigger);

    }

}